home *** CD-ROM | disk | FTP | other *** search
- #include <Errors.h>
- #include <Folders.h>
- #include <LowMem.h>
- #include <Script.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <TextUtils.h>
- #include "DCon.h"
- #include "FileUtils.h"
- #include "StringUtils.h"
-
-
-
-
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- typedef union UniversalFMPB
- {
- ParamBlockRec PB;
- CInfoPBRec ciPB;
- DTPBRec dtPB;
- HParamBlockRec hPB;
- CMovePBRec cmPB;
- WDPBRec wdPB;
- FCBPBRec fcbPB;
- } UniversalFMPB;
-
- typedef struct DeleteEnumGlobals
- {
- OSStatus err;
- Str63 itemName;
- UniversalFMPB myPB;
- } DeleteEnumGlobals;
-
- static OSStatus DetermineVRefNum(StringPtr pathname,short vRefNum,short *realVRefNum);
- static OSStatus DeleteDirectoryContents(short vRefNum,long dirID,StringPtr name);
- static void DeleteLevel(long dirToDelete,DeleteEnumGlobals *theGlobals);
- static OSStatus GetCatInfoNoName(short vRefNum,long dirID,StringPtr name,CInfoPBPtr pb);
- static OSStatus GetVolumeInfoNoName(StringPtr pathname,short vRefNum,HParmBlkPtr pb);
-
- #ifdef __cplusplus
- }
- #endif
-
-
-
-
-
- OSStatus CreateTempDirectory(short *vRefNum,long *parID,StringPtr name,long *dirID)
- {
- FSSpec tmpSpec;
- short tmpVRefNum;
- long tmpParID,tmpDirID;
- UInt32 secs;
- OSStatus err;
-
-
- // Sanity check input parameters.
- if_dAssertIfTrue(!vRefNum || ((!parID || !name) && !dirID))
- return paramErr;
-
- err = FindFolder(kOnSystemDisk,kTemporaryFolderType,kCreateFolder,&tmpVRefNum,&tmpParID);
- if (err != noErr)
- return err;
-
- GetDateTime(&secs);
- srand(secs);
-
- do
- {
- tmpSpec.name[0] = sprintf((char*)&tmpSpec.name[1],"tmp%08lX%04lX%04lX",secs,LMGetTicks() & 0xFFFF,rand());
- err = FSMakeFSSpec(tmpVRefNum,tmpParID,tmpSpec.name,&tmpSpec);
- }while(err == noErr);
- if (err != fnfErr)
- return err;
-
- err = DirCreate(tmpVRefNum,tmpParID,tmpSpec.name,&tmpDirID);
- if (err != noErr)
- return err;
-
- *vRefNum = tmpVRefNum;
- if (parID) *parID = tmpParID;
- if (name) pstrcpy(name,tmpSpec.name);
- if (dirID) *dirID = tmpDirID;
-
- return noErr;
- }
-
-
-
-
-
- OSStatus CreateTempFile(short *vRefNum,long *dirID,StringPtr name)
- {
- FSSpec tmpSpec;
- short tmpVRefNum;
- long tmpDirID;
- UInt32 secs;
- OSStatus err;
-
-
- // Sanity check input parameters.
- if_dAssertIfTrue(!vRefNum || !dirID || !name)
- return paramErr;
-
- err = FindFolder(kOnSystemDisk,kTemporaryFolderType,kCreateFolder,&tmpVRefNum,&tmpDirID);
- if (err != noErr)
- return err;
-
- GetDateTime(&secs);
- srand(secs);
-
- do
- {
- tmpSpec.name[0] = sprintf((char*)&tmpSpec.name[1],"tmp%08lX%04lX%04lX",secs,LMGetTicks() & 0xFFFF,rand());
- err = FSMakeFSSpec(tmpVRefNum,tmpDirID,tmpSpec.name,&tmpSpec);
- }while(err == noErr);
- if (err != fnfErr)
- return err;
-
- // Create a raw file with anonymous name/type/creator.
- err = HCreate(tmpVRefNum,tmpDirID,tmpSpec.name,'????','????');
- if (err != noErr)
- return err;
-
- // Make sure to create the resource fork...
- HCreateResFile(tmpVRefNum,tmpDirID,tmpSpec.name);
-
- *vRefNum = tmpVRefNum;
- *dirID = tmpDirID;
- pstrcpy(name,tmpSpec.name);
-
- return noErr;
- }
-
-
-
-
-
- OSStatus DeleteDirectory(short vRefNum,long dirID,StringPtr name)
- {
- OSStatus err;
-
-
- // Sanity check input parameters.
- if_dAssertIfTrue(!name)
- return paramErr;
-
- // Make sure a directory was specified and then delete its contents.
- err = DeleteDirectoryContents(vRefNum,dirID,name);
- if (err == noErr)
- {
- err = HDelete(vRefNum,dirID,name);
- if (err == fLckdErr)
- {
- // Unlock the directory locked by AppleShare, and try again.
- HRstFLock(vRefNum,dirID,name);
- err = HDelete(vRefNum,dirID,name);
- }
- }
-
- return err;
- }
-
-
-
-
-
- OSStatus DeleteDirectoryContentsFromID(short vRefNum,long dirID)
- {
- CInfoPBRec pb;
- Str31 name;
- OSStatus err;
-
-
- name[0] = 0;
- pb.dirInfo.ioCompletion = NULL;
- pb.dirInfo.ioNamePtr = name;
- pb.dirInfo.ioFDirIndex = -1; // use ioDirID
- pb.dirInfo.ioVRefNum = vRefNum;
- pb.dirInfo.ioDrDirID = dirID;
- err = PBGetCatInfoSync(&pb);
- if (err != noErr)
- return err;
-
- return DeleteDirectoryContents(pb.dirInfo.ioVRefNum,pb.dirInfo.ioDrParID,name);
- }
-
-
-
-
-
- OSStatus DeleteDirOrFile(short vRefNum,long dirID,StringPtr name)
- {
- long theDirID;
- Boolean isDirectory;
- OSStatus err;
-
-
- // Sanity check input parameters.
- if_dAssertIfTrue(!name)
- return paramErr;
-
- err = GetDirectoryID(vRefNum,dirID,name,&theDirID,&isDirectory);
- if (err != noErr)
- return err;
-
- if (isDirectory)
- err = DeleteDirectory(vRefNum,dirID,name);
- else
- err = DeleteFile(vRefNum,dirID,name);
-
- return err;
- }
-
-
-
-
-
- OSStatus DeleteFile(short vRefNum,long dirID,StringPtr name)
- {
- short trashVRefNum;
- long trashDirID;
- UInt32 trashSecs;
- Str32 trashName;
- FSSpec trashSpec;
- FIDParam trashPB;
- OSStatus err,trashErr;
-
-
- // Sanity check input parameters.
- if_dAssertIfTrue(!name)
- return paramErr;
-
- // Try to simply delete the file.
- err = HDelete(vRefNum,dirID,name);
- if (err == fLckdErr)
- {
- // File was locked, unlock it and try again.
- HRstFLock(vRefNum,dirID,name);
- err = HDelete(vRefNum,dirID,name);
- }
-
- // Did delete fail?
- if ((err != noErr) && (err != fnfErr))
- {
- // For one reason or another we couldn't delete the file, instead
- // of blindly giving up lets try to move it to the the Trash Can.
- trashErr = FindFolder(vRefNum,kTrashFolderType,kCreateFolder,&trashVRefNum,&trashDirID);
- if (trashErr != noErr)
- return err;
-
- do
- {
- GetDateTime(&trashSecs);
- NumToString(((LMGetTicks() << 16) | (trashSecs & 0xFFFF)),trashName);
- trashErr = FSMakeFSSpec(trashVRefNum,trashDirID,trashName,&trashSpec);
- }while(trashErr == noErr);
-
- if_dAssertIfTrue(trashErr != fnfErr)
- return err;
-
- trashErr = FSpCreate(&trashSpec,'????','????',smSystemScript);
- if_dAssertIfTrue(trashErr != noErr)
- return err;
-
- trashPB.ioCompletion = NULL;
- trashPB.ioNamePtr = name;
- trashPB.ioVRefNum = vRefNum;
- trashPB.ioDestNamePtr = trashName;
- trashPB.ioDestDirID = trashDirID;
- trashPB.ioSrcDirID = dirID;
- trashErr = PBExchangeFilesSync((HParmBlkPtr)&trashPB);
- if (trashErr != noErr)
- {
- HDelete(trashVRefNum,trashDirID,trashName);
- return err;
- }
-
- err = HDelete(vRefNum,dirID,name);
- }
-
- return err;
- }
-
-
-
-
-
- OSStatus GetDirectoryID(short vRefNum,long dirID,StringPtr name,long *theDirID,Boolean *isDirectory)
- {
- CInfoPBRec pb;
- Boolean isDir;
- OSStatus err;
-
-
- // Sanity check input parameters.
- if_dAssertIfTrue(!name)
- return paramErr;
-
- err = GetCatInfoNoName(vRefNum,dirID,name,&pb);
- isDir = (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
-
- if (isDirectory != NULL)
- *isDirectory = isDir;
-
- if (theDirID != NULL)
- {
- if (isDir)
- *theDirID = pb.dirInfo.ioDrDirID;
- else
- *theDirID = pb.hFileInfo.ioFlParID;
- }
-
- return err;
- }
-
-
-
-
-
- OSStatus GetDirOrFileVisibility(short vRefNum,long dirID,StringPtr name,Boolean *isVisible)
- {
- CInfoPBRec pb;
- OSStatus err;
-
-
- // Sanity check input parameters.
- if_dAssertIfTrue(!name || !isVisible)
- return paramErr;
-
- pb.hFileInfo.ioCompletion = NULL;
- pb.hFileInfo.ioNamePtr = name;
- pb.hFileInfo.ioVRefNum = vRefNum;
- pb.hFileInfo.ioFDirIndex = 0;
- pb.hFileInfo.ioDirID = dirID;
- err = PBGetCatInfoSync(&pb);
- if (err != noErr)
- return err;
-
- *isVisible = ((pb.hFileInfo.ioFlFndrInfo.fdFlags & 0x4000) == 0);
- return noErr;
- }
-
-
-
-
-
- OSStatus SameDirOrFile(short vRefNum1,long dirID1,StringPtr name1,short vRefNum2,long dirID2,StringPtr name2,Boolean *same)
- {
- CInfoPBRec pb1,pb2;
- Str255 pname1,pname2;
- OSStatus err;
-
-
- // Sanity check input parameters.
- if_dAssertIfTrue(!name1 || !name2 || !same)
- return paramErr;
-
- pb1.hFileInfo.ioCompletion = NULL;
- pb1.hFileInfo.ioNamePtr = pstrcpy(pname1,name1);
- pb1.hFileInfo.ioVRefNum = vRefNum1;
- pb1.hFileInfo.ioFDirIndex = 0;
- pb1.hFileInfo.ioDirID = dirID1;
- err = PBGetCatInfoSync(&pb1);
- if (err != noErr)
- return err;
-
- pb2.hFileInfo.ioCompletion = NULL;
- pb2.hFileInfo.ioNamePtr = pstrcpy(pname2,name2);
- pb2.hFileInfo.ioVRefNum = vRefNum2;
- pb2.hFileInfo.ioFDirIndex = 0;
- pb2.hFileInfo.ioDirID = dirID2;
- err = PBGetCatInfoSync(&pb2);
- if (err != noErr)
- return err;
-
- *same = ((pb1.hFileInfo.ioVRefNum == pb2.hFileInfo.ioVRefNum) &&
- (pb1.hFileInfo.ioFlParID == pb2.hFileInfo.ioFlParID) &&
- !pstrcmpnc(pname1,pname2));
-
- return noErr;
- }
-
-
-
-
-
- OSStatus SetDirOrFileVisibility(short vRefNum,long dirID,StringPtr name,Boolean isVisible)
- {
- CInfoPBRec pb;
- OSStatus err;
-
-
- // Sanity check input parameters.
- if_dAssertIfTrue(!name)
- return paramErr;
-
- pb.hFileInfo.ioCompletion = NULL;
- pb.hFileInfo.ioNamePtr = name;
- pb.hFileInfo.ioVRefNum = vRefNum;
- pb.hFileInfo.ioFDirIndex = 0;
- pb.hFileInfo.ioDirID = dirID;
- err = PBGetCatInfoSync(&pb);
- if (err != noErr)
- return err;
-
- pb.hFileInfo.ioCompletion = NULL;
- pb.hFileInfo.ioNamePtr = name;
- pb.hFileInfo.ioVRefNum = vRefNum;
- pb.hFileInfo.ioFDirIndex = 0;
- pb.hFileInfo.ioDirID = dirID;
- pb.hFileInfo.ioFlFndrInfo.fdFlags = (pb.hFileInfo.ioFlFndrInfo.fdFlags & ~0x4000) | (isVisible ? 0 : 0x4000);
- err = PBSetCatInfoSync(&pb);
- if (err != noErr)
- return err;
-
- return noErr;
- }
-
-
-
-
-
- OSStatus FSCreateTempDirectory(FSSpec *tmpDir,long *dirID)
- {
- // Sanity check input parameters.
- if_dAssertIfTrue(!tmpDir)
- return paramErr;
-
- return CreateTempDirectory(&tmpDir->vRefNum,&tmpDir->parID,tmpDir->name,dirID);
- }
-
-
-
-
-
- OSStatus FSCreateTempFile(FSSpec *tmpFile)
- {
- // Sanity check input parameters.
- if_dAssertIfTrue(!tmpFile)
- return paramErr;
-
- return CreateTempFile(&tmpFile->vRefNum,&tmpFile->parID,tmpFile->name);
- }
-
-
-
-
-
- OSStatus FSDeleteDirectory(FSSpec *dirSpec)
- {
- // Sanity check input parameters.
- if_dAssertIfTrue(!dirSpec)
- return paramErr;
-
- return DeleteDirectory(dirSpec->vRefNum,dirSpec->parID,dirSpec->name);
- }
-
-
-
-
-
- OSStatus FSDeleteDirOrFile(FSSpec *dirOrFileSpec)
- {
- // Sanity check input parameters.
- if_dAssertIfTrue(!dirOrFileSpec)
- return paramErr;
-
- return DeleteDirOrFile(dirOrFileSpec->vRefNum,dirOrFileSpec->parID,dirOrFileSpec->name);
- }
-
-
-
-
-
- OSStatus FSDeleteFile(FSSpec *fileSpec)
- {
- // Sanity check input parameters.
- if_dAssertIfTrue(!fileSpec)
- return paramErr;
-
- return DeleteFile(fileSpec->vRefNum,fileSpec->parID,fileSpec->name);
- }
-
-
-
-
-
- OSStatus FSGetDirectoryID(FSSpec *dirOrFileSpec,long *theDirID,Boolean *isDirectory)
- {
- // Sanity check input parameters.
- if_dAssertIfTrue(!dirOrFileSpec)
- return paramErr;
-
- return GetDirectoryID(dirOrFileSpec->vRefNum,dirOrFileSpec->parID,dirOrFileSpec->name,theDirID,isDirectory);
- }
-
-
-
-
-
- OSStatus FSGetDirOrFileVisibility(FSSpec *dirOrFileSpec,Boolean *isVisible)
- {
- // Sanity check input parameters.
- if_dAssertIfTrue(!dirOrFileSpec || !isVisible)
- return paramErr;
-
- return GetDirOrFileVisibility(dirOrFileSpec->vRefNum,dirOrFileSpec->parID,dirOrFileSpec->name,isVisible);
- }
-
-
-
-
-
- OSStatus FSSameDirOrFile(FSSpec *fileOrDir1,FSSpec *fileOrDir2,Boolean *same)
- {
- // Sanity check input parameters.
- if_dAssertIfTrue(!fileOrDir1 || !fileOrDir2 || !same)
- return paramErr;
-
- return SameDirOrFile(fileOrDir1->vRefNum,fileOrDir1->parID,fileOrDir1->name,
- fileOrDir2->vRefNum,fileOrDir2->parID,fileOrDir2->name,same);
- }
-
-
-
-
-
- OSStatus FSSetDirOrFileVisibility(FSSpec *dirOrFileSpec,Boolean isVisible)
- {
- // Sanity check input parameters.
- if_dAssertIfTrue(!dirOrFileSpec)
- return paramErr;
-
- return SetDirOrFileVisibility(dirOrFileSpec->vRefNum,dirOrFileSpec->parID,dirOrFileSpec->name,isVisible);
- }
-
-
- #if 0
- #pragma mark -
- #endif
-
-
- OSStatus DetermineVRefNum(StringPtr pathname,short vRefNum,short *realVRefNum)
- {
- HParamBlockRec pb;
- OSStatus err;
-
-
- err = GetVolumeInfoNoName(pathname,vRefNum,&pb);
- *realVRefNum = pb.volumeParam.ioVRefNum;
- return err;
- }
-
-
-
-
-
- OSStatus DeleteDirectoryContents(short vRefNum,long dirID,StringPtr name)
- {
- DeleteEnumGlobals theGlobals;
- Boolean isDirectory;
- OSStatus err;
-
-
- // Get the real dirID and make sure it is a directory.
- err = GetDirectoryID(vRefNum,dirID,name,&dirID,&isDirectory);
- if (err == noErr)
- {
- if (isDirectory)
- {
- // Get the real vRefNum.
- err = DetermineVRefNum(name,vRefNum,&vRefNum);
- if (err == noErr)
- {
- // Set up the globals we need to access from the recursive routine.
- theGlobals.myPB.ciPB.dirInfo.ioVRefNum = vRefNum;
-
- // Here we go into recursion land...
- DeleteLevel(dirID,&theGlobals);
- err = theGlobals.err;
- }
- }
- else
- {
- err = dirNFErr;
- }
- }
-
- return err;
- }
-
-
-
-
-
- void DeleteLevel(long dirToDelete,DeleteEnumGlobals *theGlobals)
- {
- long savedDir;
-
-
- do
- {
- // Prepare to delete directory.
- theGlobals->myPB.ciPB.dirInfo.ioNamePtr = (StringPtr)&theGlobals->itemName;
- theGlobals->myPB.ciPB.dirInfo.ioFDirIndex = 1; // get first item
- theGlobals->myPB.ciPB.dirInfo.ioDrDirID = dirToDelete;
- theGlobals->err = PBGetCatInfoSync(&(theGlobals->myPB.ciPB));
- if (theGlobals->err == noErr)
- {
- savedDir = dirToDelete;
-
- // We have an item. Is it a file or directory?
- if ((theGlobals->myPB.ciPB.dirInfo.ioFlAttrib & ioDirMask) != 0)
- {
- // It's a directory.
- savedDir = theGlobals->myPB.ciPB.dirInfo.ioDrDirID; // save dirID of directory instead
- DeleteLevel(theGlobals->myPB.ciPB.dirInfo.ioDrDirID,theGlobals); // Delete its contents
- theGlobals->myPB.ciPB.dirInfo.ioNamePtr = NULL; // prepare to delete directory
- }
-
- if (theGlobals->err == noErr)
- {
- theGlobals->myPB.ciPB.dirInfo.ioDrDirID = savedDir; // restore dirID
- theGlobals->myPB.hPB.fileParam.ioFVersNum = 0; // just in case it's used on an MFS volume...
- theGlobals->err = PBHDeleteSync(&(theGlobals->myPB.hPB)); // delete this item
- if (theGlobals->err != noErr)
- {
- theGlobals->err = DeleteFile(theGlobals->myPB.hPB.fileParam.ioVRefNum,
- theGlobals->myPB.ciPB.dirInfo.ioDrDirID,
- theGlobals->myPB.ciPB.dirInfo.ioNamePtr);
- }
- }
- }
- }while(theGlobals->err == noErr);
-
- if (theGlobals->err == fnfErr)
- theGlobals->err = noErr;
- }
-
-
-
-
-
- OSStatus GetCatInfoNoName(short vRefNum,long dirID,StringPtr name,CInfoPBPtr pb)
- {
- Str31 tempName;
- OSStatus err;
-
-
- // Protection against File Sharing problem.
- if ((name == NULL) || (name[0] == 0))
- {
- tempName[0] = 0;
- pb->dirInfo.ioNamePtr = tempName;
- pb->dirInfo.ioFDirIndex = -1; // use ioDirID
- }
- else
- {
- pb->dirInfo.ioNamePtr = (StringPtr)name;
- pb->dirInfo.ioFDirIndex = 0; // use ioNamePtr and ioDirID
- }
-
- pb->dirInfo.ioVRefNum = vRefNum;
- pb->dirInfo.ioDrDirID = dirID;
- err = PBGetCatInfoSync(pb);
- pb->dirInfo.ioNamePtr = NULL;
-
- return err;
- }
-
-
-
-
-
- OSStatus GetVolumeInfoNoName(StringPtr pathname,short vRefNum,HParmBlkPtr pb)
- {
- Str255 tempPathname;
- OSStatus err;
-
-
- // Make sure pb parameter is not NULL.
- if (pb != NULL)
- {
- pb->volumeParam.ioVRefNum = vRefNum;
- if (pathname == NULL)
- {
- pb->volumeParam.ioNamePtr = NULL;
- pb->volumeParam.ioVolIndex = 0; // use ioVRefNum only
- }
- else
- {
- BlockMoveData(pathname,tempPathname,pathname[0] + 1); // make a copy of the string and
- pb->volumeParam.ioNamePtr = (StringPtr)tempPathname; // use the copy so original isn't trashed
- pb->volumeParam.ioVolIndex = -1; // use ioNamePtr/ioVRefNum combination
- }
-
- err = PBHGetVInfoSync(pb);
- pb->volumeParam.ioNamePtr = NULL; // ioNamePtr may point to local tempPathname, so don't return it
- }
- else
- err = paramErr;
-
- return err;
- }
-